package com.weiwei.easyapiinterface.controller;

import cn.hutool.http.HttpUtil;
import com.weiwei.easyapiclientsdk.model.WeatherParams;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@RestController
@RequestMapping("/weather")
public class WeatherController {
    
    @PostMapping("/")
    public String getWeatherByPost(@RequestBody(required = false) WeatherParams weatherParams, HttpServletRequest request) {
        String weatherUrl = "https://api.vvhan.com/api/weather";
        HashMap<String, Object> paramMap = new HashMap<>();
        if (weatherParams == null) {
            weatherParams = new WeatherParams();
        }
        if (weatherParams.getCity() == null || ("").equals(weatherParams.getCity())) {
            return "{\"error\": \"参数不能为空\"}";
        } else {
            paramMap.put("city",weatherParams.getCity());  
        }
        return HttpUtil.get(weatherUrl, paramMap);
    }
}
